<template>
    <d2-container type="card">
        <header>
            <el-form :inline="true" :model="selectFrom" class="demo-form-inline" size="mini">
                <el-form-item label="类型">
                    <el-input v-model="selectFrom.type" placeholder="类型" clearable></el-input>
                </el-form-item>
                <el-form-item>
                    <el-button type="primary" @click="getComDistributionRuleData()">查询</el-button>
                    <el-button type="primary" @click="addComDistributionRuleData()">新增</el-button>
                </el-form-item>
            </el-form>
        </header>
        <footer>
            <el-table :data="tableData" border style="width: 100%" size="mini" :height="height" v-loading="tableloading">
                <el-table-column prop="id" label="分销商等级规则ID" align="center">
                </el-table-column>
                <el-table-column prop="grade" label="等级" align="center">
                </el-table-column>
                <el-table-column prop="commission" label="提成比例" align="center">
                </el-table-column>
                <el-table-column prop="income" label="升级收益条件" align="center">
                </el-table-column>
                <el-table-column prop="type" label="类型" align="center">
                    <template slot-scope="scope">
                        <div v-if="scope.row.type == 0">普通分销商</div>
                        <div v-if="scope.row.type == 1">专业分销商</div>
                    </template>
                </el-table-column>
            </el-table>
            <div class="block" style="text-align:center">
                <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange" :current-page="page"
                    :page-sizes="[5, 10, 15, 20]" :page-size="limit" layout="total, sizes, prev, pager, next, jumper"
                    :total="total">
                </el-pagination>
            </div>
        </footer>
        <el-dialog :title="fromTitle" :visible.sync="fromSign" width="30%" :close-on-click-modal="false"
            :close-on-press-escape="false" :show-close="false">
            <el-form :model="from" :rules="rules" ref="getComDistributionRuleFrom" label-width="100px" class="demo-ruleForm" size="mini">
                <el-form-item label="分销商等级规则名称" prop="name">
                    <el-input v-model="from.name"></el-input>
                </el-form-item>
                <el-form-item label="位置" prop="address">
                    <el-input type="textarea" v-model="from.address" maxlength="100" show-word-limit></el-input>
                </el-form-item>
            </el-form>
            <span slot="footer" class="dialog-footer">
                <el-button @click="closeComDistributionRule()" size="mini">取 消</el-button>
                <el-button type="primary" @click="submit()" size="mini" v-loading="loading">确 定</el-button>
            </span>
        </el-dialog>
    </d2-container>
</template>
  
<script>
import { getComDistributionRule, addComDistributionRule, updateComDistributionRule, deleteComDistributionRule } from "@/api/modules/com_distribution_rule.js"
export default {
    name: 'comDistributionRule',
    data() {
        return {
            selectFrom: {},//搜索栏
            tableData: [],//表格数据
            page: 1,
            limit: 10,
            total: 0,
            height: window.innerHeight - 250, //表格高度
            fromSign: false,//添加表单标记
            fromTitle: "添加分销商等级规则",
            from: {},//分销商等级规则数据
            isUpdate: false,//是否修改
            rules: {
                name: [
                    { required: true, message: '请输入分销商等级规则名称', trigger: 'blur' },
                    { min: 1, max: 30, message: '长度在 1 到 30 个字符', trigger: 'blur' }
                ],
                address: [
                    { required: true, message: '请输入分销商等级规则位置', trigger: 'blur' },
                    { min: 1, max: 100, message: '长度在 1 到 100 个字符', trigger: 'blur' }
                ]
            },
            loading: false,//添加表格
            tableloading:false,//表格数据
        }
    },
    mounted() {
        this.getComDistributionRuleData();

        //-------------------------表格响应----------------//
        this.createHeight();
        //当页面发生变化时，表格高度固定
        window.onresize = () => {
            return (() => {
                this.$nextTick(() => {
                    this.createHeight();
                });
            })();
        };
    },
    methods: {
        //解冻分销商等级规则
        thawComDistributionRuleData(row){
            thawcomDistributionRule(row).then(res =>{
                this.getComDistributionRuleData();
            })
        },
        //冻结分销商等级规则
        freezeComDistributionRuleData(row) {
            freezecomDistributionRule(row).then(res => {
                this.getComDistributionRuleData();
            })
        },
        //修改分销商等级规则
        updateComDistributionRuleData(row) {
            this.fromTitle = "修改分销商等级规则";
            this.fromSign = true;
            this.isUpdate = true;
            this.from = JSON.parse(JSON.stringify(row));
        },
        //提交
        submit() {
            this.$refs['getComDistributionRuleFrom'].validate((valid) => {
                if (valid) {
                    if (this.isUpdate) {
                        this.loading = true;
                        updatecomDistributionRule(this.from).then(res => {
                            if (res.code == 200) {
                                this.$notify({
                                    title: '成功',
                                    message: '修改成功',
                                    type: 'success'
                                });
                            } else {
                                this.$notify.error({
                                    title: '错误',
                                    message: res.message
                                });
                            }
                            this.closecomDistributionRule();
                            this.getComDistributionRuleData();
                        })
                    } else {
                        this.loading = true;
                        addcomDistributionRule(this.from).then(res => {
                            if (res.code == 200) {
                                this.$notify({
                                    title: '成功',
                                    message: '添加成功',
                                    type: 'success'
                                });
                            } else {
                                this.$notify.error({
                                    title: '错误',
                                    message: res.message
                                });
                            }
                            this.closecomDistributionRule();
                            this.getComDistributionRuleData();
                        })
                    }
                } else {
                    return false;
                }
            });
        },

        //添加分销商等级规则
        addComDistributionRuleData() {
            this.fromTitle = "添加分销商等级规则";
            this.fromSign = true;
            this.isUpdate = false;
        },
        //关闭表单
        closeComDistributionRule() {
            this.fromSign = false;
            this.loading = false;
            this.cleanComDistributionRule();
        },
        //清空表单
        cleanComDistributionRule() {
            this.from = {};
            if (this.$refs['getComDistributionRuleFrom']) {
                this.$refs['getComDistributionRuleFrom'].resetFields();
            }
        },
        //获取分销商等级规则数据
        getComDistributionRuleData() {
            this.selectFrom.page = this.page;
            this.selectFrom.limit = this.limit;
            this.tableloading = true;
            getComDistributionRule(this.selectFrom).then(res => {
                this.tableData = res.dataList;
                this.total = res.total;
                this.tableloading = false;
            })
        },
        //改变条数
        handleSizeChange(val) {
            this.limit = val;
            this.getComDistributionRuleData();
        },
        //改变页数
        handleCurrentChange(val) {
            this.page = val;
            this.getComDistributionRuleData();
        },

        //---------------自动变更表格高度------------------//
        createHeight() {
            this.height = window.innerHeight - 250;
        }
    }
}
</script>
  